package com.sinvo.oa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sinvo.oa.entity.model.system.SysDept;
import com.sinvo.oa.exception.CustomException;
import com.sinvo.oa.mapper.SysDeptMapper;
import com.sinvo.oa.result.ResultCode;
import com.sinvo.oa.service.SysDeptService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinvo.oa.util.TreeUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description SysDeptService实现类
 */
@Service
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> implements SysDeptService {

    @Override
    public List<SysDept> findAllDeptNodes() {
        LambdaQueryWrapper<SysDept> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(SysDept::getSortValue);
        List<SysDept> deptList = this.list(queryWrapper);
        List<SysDept> deptTreeList = TreeUtils.listToTree(deptList, "parentId", "id");
        return deptTreeList;
    }

    @Override
    public void saveDept(SysDept sysDept) {
        //查询所有部门名称为输入值的
        LambdaQueryWrapper<SysDept> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysDept::getName, sysDept.getName());
        List<SysDept> deptList = this.list(queryWrapper);
        //获取它们的父id
        List<Long> parentIdList = deptList.stream().map(item -> item.getParentId()).collect(Collectors.toList());
        //和输入数据有同一父id 则重复
        if (parentIdList.contains(sysDept.getParentId())){
            throw new CustomException(ResultCode.DATA_ERROR.getCode(), "部门名重复");
        }
        boolean success = this.save(sysDept);
        if (!success) {
            throw new CustomException(ResultCode.SERVICE_ERROR.getCode(), "新增失败，请重试");
        }
    }

    @Override
    public void updateDept(SysDept sysDept) {
        //查询所有部门名称为输入值的
        LambdaQueryWrapper<SysDept> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysDept::getName, sysDept.getName());
        List<SysDept> deptList = this.list(queryWrapper);
        //获取它们的父id
        List<Long> parentIdList = deptList.stream().map(item -> item.getParentId()).collect(Collectors.toList());
        //获取id
        List<Long> idList = deptList.stream().map(item -> item.getId()).collect(Collectors.toList());
        //和输入数据有同一父id 则重复
        if (parentIdList.contains(sysDept.getParentId()) && !idList.contains(sysDept.getId())){
            throw new CustomException(ResultCode.DATA_ERROR.getCode(), "部门名重复");
        }
        boolean success = this.updateById(sysDept);
        if (!success) {
            throw new CustomException(ResultCode.SERVICE_ERROR.getCode(), "新增失败，请重试");
        }
    }

    @Override
    public void removeDeptById(Long id) {
        //查询是否有部门的parentId为 要删除部门的id
        LambdaQueryWrapper<SysDept> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysDept::getParentId, id);
        int count = this.count(queryWrapper);
        //有说明存在子部门
        if (count > 0) {
            throw new CustomException(ResultCode.SERVICE_ERROR.getCode(), "存在子菜单，不能删除");
        }
        this.removeById(id);
    }

    @Override
    public boolean changeStatus(Long id, Integer status) {
        //根据id获取部门
        SysDept sysDept = this.getById(id);
        //修改状态
        sysDept.setStatus(status);
        this.updateById(sysDept);
        return true;
    }

}
